package com.imessage.action.manage.role;

import com.bean.ManageResource;
import com.bean.ManageResourceRole;
import com.bean.ManageRole;
import com.bean.ManageRoleUser;
import com.imessage.action.BaseAction;
import com.imessage.type.BaseState;
import com.imessage.vo.RoleVo;
import com.imessage.web.HttpJson;
import com.imessage.web.Page;
import io.ebean.Expr;
import io.ebean.Expression;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

public class RoleBaseAction extends BaseAction {

    protected final String datagrid="datagrid-manageRole-filter";


    protected HttpJson checkData(RoleVo roleVo){
        if(roleVo.getMenuid()==null||roleVo.getMenuid().length==0){
            return getJsonForErr("未选择菜单", datagrid);
        }
        return null;
    }


    protected boolean existRole(String code,Long manage_role_id){
        List<Expression> expressionList=new ArrayList<Expression>();
        if(StringUtils.isNotBlank(code)){
            expressionList.add(Expr.eq("code", code));
        }
        if(manage_role_id!=null){
            expressionList.add(Expr.ne("id", manage_role_id));
        }
        ManageRole manageRole =baseService.findObject(ManageRole.class,expressionList);
        if(manageRole==null){
            return false;
        }
        return true;
    }


    protected RoleBean getRoleBean(ManageRole manageRole){
       RoleBean roleBean=new RoleBean();
        roleBean.setManageRole(manageRole);
        List<ManageResource> manageMenuList=roleMenuService.findManageMenuList(manageRole.getId());
        StringBuffer sb=new StringBuffer();
        for(ManageResource manageMenu:manageMenuList){
            sb.append(manageMenu.getName());
            sb.append(",");
        }
        roleBean.setManageMenus(sb.toString());
        roleBean.setId(manageRole.getId().toString());
        return roleBean;
    }


    protected Page<RoleBean> getRoleBeanPage(Page<ManageRole> page){
        Page<RoleBean> roleBeanPage=new Page<RoleBean>();
        roleBeanPage.setTotalRow(page.getTotalRow());
        roleBeanPage.setTotal(page.getTotal());
        roleBeanPage.setPageCurrent(page.getPageCurrent());
        roleBeanPage.setExtra(page.getExtra());
        roleBeanPage.setPageSize(page.getPageSize());
        roleBeanPage.setTotalPage(page.getTotalPage());
        roleBeanPage.setList(new ArrayList<RoleBean>());
        if(page.getList()==null||page.getList().size()==0){
            return roleBeanPage;
        }
        List<RoleBean> roleBeanList=new ArrayList<RoleBean>();
        for(ManageRole manageRole:page.getList()){
            RoleBean roleBean=new RoleBean();
            roleBean.setManageRole(manageRole);
            List<ManageResource> manageMenuList=
                    roleMenuService.findManageMenuList(manageRole.getId());
            StringBuffer sb=new StringBuffer();
            for(ManageResource manageMenu:manageMenuList){
                sb.append(manageMenu.getName());
                sb.append(",");
            }
            roleBean.setManageMenus(sb.toString());
            roleBean.setId(manageRole.getId().toString());
            roleBeanList.add(roleBean);
        }
        roleBeanPage.setList(roleBeanList);
        return roleBeanPage;
    }

    protected void setExpressionList(List<Expression> expressionList,RoleVo roleVo){
        if(StringUtils.isNotBlank(roleVo.getName())){
            expressionList.add(Expr.contains("name", roleVo.getName()));
        }
    }



    protected List<ManageResourceRole> getManageRoleMenu(Long manage_role_id){
        List<Expression> expressionList=new ArrayList<Expression>();
        expressionList.add(Expr.eq("manageRoleId", manage_role_id));
        return baseService.findList(ManageResourceRole.class, expressionList);
    }


    protected List<ManageResource> getAllManageMenuList(){
        List<Expression> expressionList=new ArrayList<Expression>();
        return baseService.findList(ManageResource.class, expressionList,
                null, "sort asc");
    }


    protected List<ManageResource> sort(List<ManageResource> menuList){
        Collections.sort(menuList,new Comparator<Object>(){
            public int compare(Object o1, Object o2) {
                long b1 = ((ManageResource)o1).getSort();
                long b2 = ((ManageResource)o2).getSort();
                if(b1>b2){
                    return 1;
                }
                return -1;
            }
        });
        return menuList;
    }

    protected void saveManageRoleMenu(String[] menuid,Long manage_role_id){
        if(menuid==null || menuid.length==0){
            return ;
        }
        for(String id:menuid){
            ManageResource manageMenu=baseService.findById(ManageResource.class,id);
            ManageResourceRole manageRoleMenu=new ManageResourceRole();
            manageRoleMenu.setManageResourceId(manageMenu.getId());
            manageRoleMenu.setManageRoleId(manage_role_id);
            baseService.saveObject(manageRoleMenu);
        }
    }


    protected  List<ManageRoleUser> getManageUserRole(Long manage_role_id){
        List<Expression> expressionList=new ArrayList<Expression>();
        expressionList.add(Expr.eq("manageRoleId", manage_role_id));
        return baseService.findList(ManageRoleUser.class, expressionList);
    }

    /**
     * 添加修改角色，基础数据
     */
    protected void setModelAndView(ModelAndView modelAndView, ManageRole manageRole, String manageMenuIds){
        List<ManageResource> allManageMenuList=getAllManageMenuList();//所有菜单
        List<ManageResource> parentMenuList=new ArrayList<ManageResource>();

        Map<Long,List<ManageResource>> manageMenuMap=new HashMap<Long, List<ManageResource>>();

        for(ManageResource manageMenu:allManageMenuList){
            if(manageMenu.getManageHighResourceId()==0){
                parentMenuList.add(manageMenu);
            }else{
                List<ManageResource> manageMenuList=new ArrayList<ManageResource>();

                if(manageMenuMap.containsKey(manageMenu.getManageHighResourceId())){
                    manageMenuList=manageMenuMap.get(manageMenu.getManageHighResourceId());
                }
                manageMenuList.add(manageMenu);
                manageMenuMap.put(manageMenu.getManageHighResourceId(), manageMenuList);
            }
        }
        modelAndView.addObject("manageRoleForm", manageRole);
        modelAndView.addObject("manageMenuIds",manageMenuIds);
        modelAndView.addObject("manageMenuList", sort(parentMenuList));
        modelAndView.addObject("manageMenuMap", manageMenuMap);
        modelAndView.addObject("statusList", BaseState.values());
    }

    protected void setManageRole(RoleVo roleVo, ManageRole manageRole){
        copyProperties(roleVo, manageRole);
        if(StringUtils.isNotBlank(roleVo.getState())){
            manageRole.setState(BaseState.fromValue(roleVo.getState()));
        }
    }





}
